<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入企业名称/法定负责人/所属行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营状态" prop="operatingStatus">
        <el-select v-model="queryParams.operatingStatus" placeholder="请选择经营状态" clearable>
          <el-option
            v-for="dict in dict.type.enterprise_operating_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属地区" prop="affiliatingArea">
        <el-select v-model="queryParams.affiliatingArea" placeholder="请选择所属地区" clearable>
          <el-option
            v-for="dict in dict.type.affiliating_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="录入时间">
        <el-form-item prop="startTime">
          <el-date-picker clearable
                          v-model="queryParams.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="开始时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item prop="endTime">
          <el-date-picker clearable
                          v-model="queryParams.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:enterpriseInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:enterpriseInfo:edit']"
        >修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterpriseInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="10" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" :show-overflow-tooltip="true" />
      <el-table-column label="法定代表人" align="center" prop="representative" :show-overflow-tooltip="true"/>
      <el-table-column label="成立日期" align="center" prop="establishmentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.establishmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属行业" align="center" prop="industryInvolved" :show-overflow-tooltip="true" />
      <el-table-column label="所属地区" align="center" prop="affiliatingArea">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.affiliating_area" :value="scope.row.affiliatingArea"/>
        </template>
      </el-table-column>
      <el-table-column label="经营状态" align="center" prop="operatingStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.enterprise_operating_state" :value="scope.row.operatingStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="录入人" align="center" prop="clerk" :show-overflow-tooltip="true"/>
      <el-table-column label="营销人" align="center" prop="matchMan" :show-overflow-tooltip="true"/>
      <el-table-column label="录入时间" align="center" prop="entryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:enterpriseInfo:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDetails(scope.row)"
            v-hasPermi="['business:enterpriseInfo:details']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="法定代表人" prop="representative">
          <el-input v-model="form.representative" placeholder="请输入法定代表人" />
        </el-form-item>
        <el-form-item label="成立日期" prop="establishmentTime">
          <el-date-picker clearable
            v-model="form.establishmentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择成立日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经营状态" prop="operatingStatus" v-if="form.id != null">
          <el-select v-model="form.operatingStatus" placeholder="请选择经营状态">
            <el-option
              v-for="dict in dict.type.enterprise_operating_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属地区" prop="affiliatingArea">
          <el-select v-model="form.affiliatingArea" placeholder="请选择所属地区">
            <el-option
              v-for="dict in dict.type.affiliating_area"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属行业" prop="industryInvolved">
          <el-input v-model="form.industryInvolved" placeholder="请输入所属行业" />
        </el-form-item>


        <el-form-item label="注册资本" prop="registeredCapital">
          <el-input v-model="form.registeredCapital" placeholder="请输入注册资本" />
        </el-form-item>
        <el-form-item label="注册地址" prop="registeredAddress" >
          <el-input v-model="form.registeredAddress" placeholder="请输入注册地址" type="textarea" :rows="5" />
        </el-form-item>
        <el-form-item label="经营范围" prop="businessScope" >
          <el-input v-model="form.businessScope" placeholder="请输入经营范围" type="textarea" :rows="5"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="客户详情" :visible.sync="detailsOpen" width="1200px" append-to-body>
      <el-table v-loading="loading" :data="detailsList">
        <el-table-column label="企业名称" align="center" prop="enterpriseName" :show-overflow-tooltip="true" />
        <el-table-column label="法定代表人" align="center" prop="representative" :show-overflow-tooltip="true"/>
        <el-table-column label="成立日期" align="center" prop="establishmentTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.establishmentTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属行业" align="center" prop="industryInvolved" :show-overflow-tooltip="true" />
        <el-table-column label="所属地区" align="center" prop="affiliatingArea">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.affiliating_area" :value="scope.row.affiliatingArea"/>
          </template>
        </el-table-column>
        <el-table-column label="经营状态" align="center" prop="operatingStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.enterprise_operating_state" :value="scope.row.operatingStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="注册资本" align="center" prop="registeredCapital"/>
        <el-table-column label="注册地址" align="center" prop="registeredAddress" :show-overflow-tooltip="true"/>
        <el-table-column label="经营范围" align="center" prop="businessScope" :show-overflow-tooltip="true"/>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailsCancel">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEnterpriseInfo, getEnterpriseInfo, delEnterpriseInfo, addEnterpriseInfo, updateEnterpriseInfo,getEnterpriseInfoDetails } from "@/api/business/enterpriseInfo";

export default {
  name: "EnterpriseInfo",
  dicts: ['enterprise_operating_state','affiliating_area'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户基本信息表格数据
      enterpriseInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 经营范围时间范围
      daterangeEntryTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: null,
        affiliatingArea: null,
        operatingStatus: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      detailsOpen : false,
      detailsList : [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户基本信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      listEnterpriseInfo(this.queryParams).then(response => {
        this.enterpriseInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseName: null,
        representative: null,
        establishmentTime: null,
        industryInvolved: null,
        affiliatingArea: null,
        operatingStatus: null,
        clerk: null,
        matchMan: null,
        entryTime: null,
        registeredCapital: null,
        registeredAddress: null,
        businessScope: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEntryTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEnterpriseInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户基本信息";
      });
    },
    /** 修改按钮操作 */
    handleDetails(row) {
      this.reset();
      const id = row.id || this.ids
      getEnterpriseInfoDetails(id).then(response => {
        this.detailsList = response.rows;
        this.detailsOpen = true;
      });
    },
    detailsCancel() {
      this.detailsOpen = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEnterpriseInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEnterpriseInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

  }
};
</script>
